package com.swufe.lianxi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MyTask extends AppCompatActivity implements Runnable{

    private static final String TAG = "MyTask";
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_task);

    }
public void setHandler(Handler handler){this.handler=handler;}
    @Override
    public void run() {
        Log.i(TAG, "run: ...");
        List<String> ratelist = new ArrayList<>();
        List<String> rateList = new ArrayList<>();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Bundle bundle = new Bundle();
        Document doc = null;
        try {
            String url = "https://www.boc.cn/sourcedb/whpj/ ";
            doc = Jsoup.connect(url).get();
            Log.i(TAG, "run: " + doc.title());
            Elements tables = doc.getElementsByTag("table");
            Element table1 = tables.get(1);
            Log.i(TAG, "run: table1=" + table1);
            //Element table6 = tables.get(5);
            //Log.i(TAG, "run: table6=" + table6);
            //获取TD中的数据
            //Elements tds = table6.getElementsByTag("td");
            Elements tds = table1.getElementsByTag("td");
            for (int i = 0; i < tds.size(); i += 8) {
                Element td1 = tds.get(i);
                Element td2 = tds.get(i + 5);
                String str1 = td1.text();
                String val = td2.text();
                Log.i(TAG, "run: " + str1 + "==>" + val);
                ratelist.add(str1 + "==>" + val);//敲重点，下次不能忘了
            }
            Message msg = handler.obtainMessage(5);
            msg.obj = ratelist;
            handler.sendMessage(msg);
            // msg.obj = rateList;
            // handler.sendMessage(msg);
            //Log.i("thread","sendMessage....");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}